package com.wfcm.service.impl;

import com.wfcm.dao.WfMemberCouponsDao;
import com.wfcm.entity.WfMemberCouponsEntity;
import com.wfcm.exception.WFCMException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.wfcm.dao.WfShoppingCartDao;
import com.wfcm.entity.WfShoppingCartEntity;
import com.wfcm.service.WfShoppingCartService;
import org.springframework.transaction.annotation.Transactional;


@Service("wfShoppingCartService")
public class WfShoppingCartServiceImpl implements WfShoppingCartService {
	@Autowired
	private WfShoppingCartDao wfShoppingCartDao;
	@Autowired
	private WfMemberCouponsDao wfMemberCouponsDao;

	@Override
	public WfShoppingCartEntity queryObject(Integer id){
		return wfShoppingCartDao.queryObject(id);
	}
	
	@Override
	public List<WfShoppingCartEntity> queryList(Map<String, Object> map){
		return wfShoppingCartDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return wfShoppingCartDao.queryTotal(map);
	}
	
	@Override
	public void save(WfShoppingCartEntity wfShoppingCart){
		wfShoppingCartDao.save(wfShoppingCart);
	}

	@Override
	public void saveAndGive(Integer userId,Integer type,Integer foreignId ,String price) throws WFCMException{
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date beginDate = null;
		Date endDate = null;
		try {
			beginDate =  formatter.parse("2018-08-27 20:00:00");
			endDate =  formatter.parse("2018-09-03 23:00:00");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		//奖励大语文类优惠券
		WfMemberCouponsEntity memberCouponsEntity = new WfMemberCouponsEntity();
		memberCouponsEntity.setUserid(userId);
		memberCouponsEntity.setTitle("满2000减300");
		memberCouponsEntity.setCost(300);
		memberCouponsEntity.setFtype(2000);
		memberCouponsEntity.setLimits(1);
		memberCouponsEntity.setBeginDate(beginDate);
		memberCouponsEntity.setEndDate(endDate);
		memberCouponsEntity.setUpdateTime(new Date());
		memberCouponsEntity.setStatus(2);
		memberCouponsEntity.setCouponsSource(2);//活动领取
		memberCouponsEntity.setReceiveTime(new Date());
		memberCouponsEntity.setValueIds("");
		wfMemberCouponsDao.save(memberCouponsEntity);

		try{
			WfShoppingCartEntity shoppingCart = new WfShoppingCartEntity();
			shoppingCart.setUserid(userId);
			shoppingCart.setType(Integer.valueOf(type));
			shoppingCart.setForeignid(Integer.valueOf(foreignId));
			shoppingCart.setCreateTime(new Date());
			shoppingCart.setPrice(price);
			shoppingCart.setNumber(1);//默认为1
			shoppingCart.setSaleflag(0);//默认为0
			wfShoppingCartDao.save(shoppingCart);
		}catch(RuntimeException e) {
			Throwable cause = e.getCause();
			if(cause instanceof com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException) {
				String errMsg = cause.getMessage();
				System.out.println("**********************************");
				System.out.println(errMsg);
				if(StringUtils.isNotBlank(errMsg) && errMsg.indexOf("Duplicate entry")!=-1) {
					throw new WFCMException(500,"亲，不能重复加入购物车哦!");
				}
			}
			throw e;
		}

	}

	
	@Override
	public void update(WfShoppingCartEntity wfShoppingCart){
		wfShoppingCartDao.update(wfShoppingCart);
	}
	
	@Override
	public void delete(Integer id){
		wfShoppingCartDao.delete(id);
	}
	
	@Override
	public void deleteGoods(Map<String, Object> map){
		wfShoppingCartDao.deleteGoods(map);
	}
	
	@Override
	public void deleteBatch(Integer[] ids){
		wfShoppingCartDao.deleteBatch(ids);
	}

	@Override
	public List<WfShoppingCartEntity> queryCourseList(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return wfShoppingCartDao.queryCourseList(map);
	}

	@Override
	public List<WfShoppingCartEntity> queryChapterList(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return wfShoppingCartDao.queryChapterList(map);
	}

	@Override
	public List<WfShoppingCartEntity> queryVipCardList(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return wfShoppingCartDao.queryVipCardList(map);
	}
	
}
